import 'package:flutter/material.dart';
import 'package:flutter_fun_app/page/musicPage.dart';
import 'package:flutter_fun_app/page/newsPage.dart';
import 'package:flutter_fun_app/page/videoPage.dart';
import 'package:flutter_fun_app/page/userPage.dart';
import 'package:flutter_common/config/config.dart';
import 'package:fluintl/fluintl.dart';
import 'package:flutter_fun_app/utils/strings.dart';

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return HomeState();
  }
}

class HomeState extends State<HomePage> {
  List<Widget> pages = List();
  var _currentIndex = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    pages..add(MusicPage())..add(NewsPage())..add(VideoPage())..add(UserPage());
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return WillPopScope(
        onWillPop: _onWillPop,
        child: Scaffold(
          body: IndexedStack(
            index: _currentIndex,
            children: pages,
          ),
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            iconSize: 24,
            currentIndex: _currentIndex,
            fixedColor: Config.titleBarColor,
            items: [
              BottomNavigationBarItem(
                  icon: Icon(Icons.queue_music),
                  title: Text(
                    IntlUtil.getString(context, Ids.bottom_bar_music),
                    style: TextStyle(fontSize: 14),
                  )),
              BottomNavigationBarItem(
                  icon: Icon(Icons.radio),
                  title: Text(
                    IntlUtil.getString(context, Ids.bottom_bar_radio),
                    style: TextStyle(fontSize: 14),
                  )),
              BottomNavigationBarItem(
                  icon: Icon(Icons.ondemand_video),
                  title: Text(
                    IntlUtil.getString(context, Ids.bottom_bar_video),
                    style: TextStyle(fontSize: 14),
                  )),
              BottomNavigationBarItem(
                  icon: Icon(Icons.supervised_user_circle),
                  title: Text(
                    IntlUtil.getString(context, Ids.bottom_bar_user),
                    style: TextStyle(fontSize: 14),
                  )),
            ],
            onTap: (int index) {
              setState(() {
                _currentIndex = index;
              });
            },
          ),
        ));
  }

  Future<bool> _onWillPop() {
    return showDialog(
          context: context,
          builder: (context) => new AlertDialog(
            title: new Text(IntlUtil.getString(context, Ids.exit_text_title)),
            content: new Text(IntlUtil.getString(context, Ids.exit_text_content)),
            actions: <Widget>[
              new FlatButton(
                onPressed: () => Navigator.of(context).pop(false),
                child: new Text(IntlUtil.getString(context, Ids.exit_text_cancel)),
              ),
              new FlatButton(
                onPressed: () => Navigator.of(context).pop(true),
                child: new Text(IntlUtil.getString(context, Ids.exit_text_confirm)),
              ),
            ],
          ),
        ) ??
        false;
  }
}
